package Frames;

import DAO.Dao;
import Entidades.Emissor;
import Entidades.Entrada;
import Entidades.TipoEntrada;
import Entidades.Usuario;
import Utils.FixedLengthDocument;
import Utils.S20BinaryLookup;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Davidson
 */
public class IfrmAlterarEntrada extends javax.swing.JInternalFrame {

    Dao dao = new Dao();
    List<Emissor> emissores = dao.listarEmissores();
    List<TipoEntrada> tipoEntrada = dao.listarTipoEntrada();
    Usuario usuario;
    Entrada entrada;
        
    public IfrmAlterarEntrada(Usuario usuario, Entrada entrada){
        initComponents();
        this.usuario = usuario;
        this.entrada = entrada;
        lbUsuario.setText(usuario.getNome());
        new S20BinaryLookup(cbEmissores);
        try {
            carregaEmissores();
            carregaTiposEntradas();
            carregaEntrada();
        } catch (SQLException ex) {
            Logger.getLogger(IfrmAlterarEntrada.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        ftfDataEntrega = new javax.swing.JFormattedTextField();
        imgDataInicial = new javax.swing.JLabel();
        cbEmissores = new javax.swing.JComboBox();
        cbTipoEntrada = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        ftfValorEntrada = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDescricao = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        txtUsuario = new javax.swing.JLabel();
        lbUsuario = new javax.swing.JLabel();
        btAlterarEntrada = new javax.swing.JButton();

        setClosable(true);
        setTitle("Alterar Entrada");
        setFrameIcon(null);
        setMinimumSize(new java.awt.Dimension(758, 361));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setText("Data da entrega:");

        ftfDataEntrega.setForeground(new java.awt.Color(102, 102, 102));
        ftfDataEntrega.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        ftfDataEntrega.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        ftfDataEntrega.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        ftfDataEntrega.setMaximumSize(new java.awt.Dimension(126, 30));
        ftfDataEntrega.setMinimumSize(new java.awt.Dimension(126, 30));
        ftfDataEntrega.setPreferredSize(new java.awt.Dimension(126, 30));

        imgDataInicial.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/calendar.png"))); // NOI18N
        imgDataInicial.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        imgDataInicial.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imgDataInicialMouseClicked(evt);
            }
        });

        cbEmissores.setEditable(true);
        cbEmissores.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbEmissores.setForeground(new java.awt.Color(41, 41, 41));
        cbEmissores.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione" }));
        cbEmissores.setMaximumSize(new java.awt.Dimension(250, 30));
        cbEmissores.setMinimumSize(new java.awt.Dimension(250, 30));
        cbEmissores.setPreferredSize(new java.awt.Dimension(250, 30));

        cbTipoEntrada.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbTipoEntrada.setForeground(new java.awt.Color(41, 41, 41));
        cbTipoEntrada.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione" }));
        cbTipoEntrada.setMaximumSize(new java.awt.Dimension(180, 30));
        cbTipoEntrada.setMinimumSize(new java.awt.Dimension(180, 30));
        cbTipoEntrada.setPreferredSize(new java.awt.Dimension(180, 30));

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 51, 51));
        jLabel4.setText("Tipo Entrada:");

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(51, 51, 51));
        jLabel5.setText("Emissor:");

        ftfValorEntrada.setDocument(new FixedLengthDocument(10));
        ftfValorEntrada.setForeground(new java.awt.Color(102, 102, 102));
        ftfValorEntrada.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("###0.00"))));
        ftfValorEntrada.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ftfValorEntrada.setText("0,00");
        ftfValorEntrada.setFont(new java.awt.Font("Calibri", 1, 18)); // NOI18N
        ftfValorEntrada.setMaximumSize(new java.awt.Dimension(100, 30));
        ftfValorEntrada.setMinimumSize(new java.awt.Dimension(100, 30));
        ftfValorEntrada.setPreferredSize(new java.awt.Dimension(100, 30));
        ftfValorEntrada.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                ftfValorEntradaFocusGained(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(51, 51, 51));
        jLabel6.setText("Valor:");

        txtDescricao.setColumns(10);
        txtDescricao.setRows(5);
        txtDescricao.setBorder(null);
        jScrollPane2.setViewportView(txtDescricao);

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(41, 41, 41));
        jLabel2.setText("Descrição:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane2))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbTipoEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbEmissores, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(ftfValorEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ftfDataEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(imgDataInicial)))
                .addContainerGap(168, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel1)
                    .addComponent(jLabel6))
                .addGap(0, 0, 0)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cbEmissores, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(ftfValorEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(ftfDataEntrega, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cbTipoEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(imgDataInicial, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(0, 0, 0)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        txtUsuario.setText("Usuário:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtUsuario)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbUsuario)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtUsuario)
                    .addComponent(lbUsuario))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btAlterarEntrada.setFont(new java.awt.Font("Calibri", 1, 12)); // NOI18N
        btAlterarEntrada.setText("Alterar");
        btAlterarEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarEntradaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btAlterarEntrada)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btAlterarEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void carregaEmissores() throws SQLException{
        Iterator<Emissor> it = emissores.iterator();
        while(it.hasNext()){
            cbEmissores.addItem(it.next().getNome());
        }
    }    

    private void carregaTiposEntradas() throws SQLException{
        Iterator<TipoEntrada> it = tipoEntrada.iterator();
        while(it.hasNext()){
            cbTipoEntrada.addItem(it.next().getNome());
        }        
    }    
    
    private void carregaEntrada(){
        cbEmissores.setSelectedItem(entrada.getNomeEmissor());
        cbTipoEntrada.setSelectedItem(entrada.getNomeTipoEntrada());
        ftfValorEntrada.setText(new DecimalFormat("#.##0,00").format(entrada.getValor()));
        ftfDataEntrega.setText(new SimpleDateFormat("dd/MM/yyyy").format(entrada.getDtReferencia()));
        txtDescricao.setText(entrada.getDescricao());
    }
    
    private void imgDataInicialMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imgDataInicialMouseClicked
        frmCalendar dialog = new frmCalendar(new javax.swing.JFrame(), false, ftfDataEntrega);
        int x = ftfDataEntrega.getLocationOnScreen().x;
        int y = ftfDataEntrega.getLocationOnScreen().y;
        dialog.setBounds(x, y + ftfDataEntrega.getHeight(), 224, 140);
        dialog.setVisible(true);
    }//GEN-LAST:event_imgDataInicialMouseClicked
    
    private void ftfValorEntradaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ftfValorEntradaFocusGained
        ftfValorEntrada.selectAll();
        
    }//GEN-LAST:event_ftfValorEntradaFocusGained

    private void btAlterarEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarEntradaActionPerformed
        /*if("Selecione".equals(cbTipoEntrada.getSelectedItem())){
            JOptionPane.showMessageDialog(rootPane, "Selecione o tipo de Entrada!");
            cbTipoEntrada.requestFocus();
            return;
        }
        if("Selecione".equals(cbEmissores.getSelectedItem())){
            JOptionPane.showMessageDialog(rootPane, "Selecione o Emissor!");
            cbEmissores.requestFocus();
            return;
        }
        if(ftfValorEntrada.getText().equals("0,00")){
            JOptionPane.showMessageDialog(rootPane, "Digite o valor!");
            ftfValorEntrada.requestFocus();
            return;
        }        
        Entrada entrada = new Entrada();
        entrada.setIdUsuario(usuario.getId());        
        try {
            entrada.setDtReferencia(new SimpleDateFormat("dd/MM/yyyy").parse(ftfDataEntrega.getText()));
        } catch (ParseException ex) {
            Logger.getLogger(IfrmAlterarEntrada.class.getName()).log(Level.SEVERE, null, ex);
        }
        Iterator<Emissor> it = emissores.iterator();
        while(it.hasNext()){
            Emissor e = it.next();
            String nome = e.getNome();
            int id = e.getId();
            if (nome.equals(cbEmissores.getSelectedItem())) {
                entrada.setIdEmissor(id);
            }
        }
        Iterator<TipoEntrada> it2 = tipoEntrada.iterator();
        while (it2.hasNext()) {
            TipoEntrada te = it2.next();
            String nome = te.getNome();            
            int id = te.getId();
            if (nome.equals(cbTipoEntrada.getSelectedItem())) {
                entrada.setIdTipo(id);
            }
        }
        entrada.setValor(Double.valueOf(ftfValorEntrada.getText().replaceAll(",", ".")));
        entrada.setDescricao(txtDescricao.getText());
        Entrada retornoEntrada = new Entrada();
        try {
            retornoEntrada = dao.lancarEntrada(entrada);
        } catch (SQLException ex) {
            Logger.getLogger(IfrmAlterarEntrada.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(IfrmAlterarEntrada.class.getName()).log(Level.SEVERE, null, ex);
        }
        */
    }//GEN-LAST:event_btAlterarEntradaActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAlterarEntrada;
    private javax.swing.JComboBox cbEmissores;
    private javax.swing.JComboBox cbTipoEntrada;
    private javax.swing.JFormattedTextField ftfDataEntrega;
    private javax.swing.JFormattedTextField ftfValorEntrada;
    private javax.swing.JLabel imgDataInicial;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbUsuario;
    private javax.swing.JTextArea txtDescricao;
    private javax.swing.JLabel txtUsuario;
    // End of variables declaration//GEN-END:variables
}

